<!-- 
 * @Author: 陆成杰
 * @Date: 2022-06-25 08:43:58
 * @LastEditTime: 2022-06-27 20:02:05
 * @LastEditors: 陆成杰
 * @FilePath: /boe/src/views/home/home-children/DeviceStatus.vue
 * Copyright (c) ${now_year} by zucc-陆成杰, All Rights Reserved. 
-->
<template>
  <div class="echart" id="mychart2" :style="myChartStyle"></div>
</template>
 
<script>
import * as echarts from "echarts";
import { getDeviceState } from "../../../axios/home";

export default {
  data() {
    return {
      xData: ["轮播测试", "123"], //横坐标
      yData: [1, 0], //数据
      myChartStyle: { float: "left", width: "200px", height: "200px" }, //图表样式
      Offline: 0,
      Online: 0,
      free: 0,
    };
  },
  mounted() {
    this.DeviceState();
    setTimeout(() => {
      this.initEcharts();
    }, 500);
  },
  methods: {
    DeviceState() {
      getDeviceState().then((res) => {
        // console.log(res);
        this.Offline = res.data.Offline;
        this.Online = res.data.Online;
        this.free = res.data.All - res.data.Online;
      });
    },
    initEcharts() {
      const option = {
        tooltip: {
          trigger: "item",
        },
        color: ["rgb(108, 116, 129)", "rgb(78, 203, 115)", "rgb(220, 187, 56)"],
        series: [
          {
            name: "设备状态",
            type: "pie",
            radius: ["40%", "70%"],
            avoidLabelOverlap: false,
            label: {
              show: true,
              position: "center",
              color: "#fff",
              formatter: "{c} 台",
            },
            emphasis: {
              label: {
                show: true,
                fontSize: "40",
                fontWeight: "bold",
              },
            },
            labelLine: {
              show: false,
            },
            // color: [rgb(41, 45, 62), rgb(24, 144, 255), rgb(220, 187, 56)]
            data: [
              { value: this.Offline, name: "离线" },
              { value: this.Online, name: "播放" },
              { value: this.free, name: "空闲" },
            ],
          },
        ],
      };
      const myChart2 = echarts.init(document.getElementById("mychart2"));
      myChart2.setOption(option);
      //随着屏幕大小调节图表
      window.addEventListener("resize", () => {
        myChart2.resize();
      });
    },
  },
};
</script>